require 'sqlexec'
require 'columns'
require 'rowinserter'

class Table
  def initialize(sqlexec, tbname)
    @sqlexec = sqlexec
    @tbname = tbname
    @colnames = getcolnames
    @cols = Columns.new(sqlexec, tbname, @colnames)
    @rowinserter = RowInserter.new(sqlexec, @tbname)
  end
  private
  def getcolnames 
    rows = @sqlexec.selquery("sp_columns #{@tbname}")
    rows.map {|r| r[3]}
  end
  public
  def rename(target)
    @sqlexec.ddlquery "sp_rename \'#{@tbname}\',\'#{target}\'"
  end
  def drop
    @sqlexec.ddlquery "DROP TABLE #{@tbname}"
  end
  def clear
    @sqlexec.ddlquery "DELETE #{@tbname}"
  end
  def to_s
    @tbname
  end
  def cols
    @cols
  end
  def [](key)
    @cols[key]
  end
  def rowinserter
    RowInserter.new(@sqlexec, @tbname)
  end
end
